package com.pw.study.gmallpublisher.controller;

import com.alibaba.fastjson.JSONObject;
import com.pw.study.gmallpublisher.enties.DUAData;
import com.pw.study.gmallpublisher.enties.GMVData;
import com.pw.study.gmallpublisher.service.DUAService;
import com.pw.study.gmallpublisher.service.ESService;
import com.pw.study.gmallpublisher.service.GMVService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DUAController {
    @Autowired
    private DUAService duAService;
    @Autowired
    private GMVService gmvService;
    @Autowired
    private ESService esService;


    /*
    /sale_detail? date=2022-01-21  &  startpage=1  &  size=5 &  keyword=小米手机
 */
    @RequestMapping(value = "/sale_detail")
    public Object handle3(String date, Integer startpage, Integer size, String keyword) throws IOException {

        return esService.getSaleDetail(date, startpage, size, keyword);

    }

    /*
  http://localhost:8070/realtime-total?date=2022-01-15
      [
          {"id":"dau","name":"当日日活数","value":1200},
          {"id":"new_mid","name":"新增设备数","value":233}
          {"id":"order_amount","name":"当日交易额","value":1000.2 }
      ]
http://localhost:8070/realtime-total?date=2022-08-18
   */
    @RequestMapping(value = "/realtime-total")
    public Object handle2(String date) {

        ArrayList<JSONObject> result = new ArrayList<>();

        Integer dauByDate = duAService.getDAUByDate(date);
        Integer newUsersCountByDate = duAService.getNewUsersCountByDate(date);
        double todayOrderlCount = gmvService.getOrderAmountByDate(date);


        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();

        jsonObject1.put("id", "dau");
        jsonObject1.put("name", "当日日活数");
        jsonObject1.put("value", dauByDate);

        jsonObject2.put("id", "new_mid");
        jsonObject2.put("name", "新增设备数");
        jsonObject2.put("value", newUsersCountByDate);

        jsonObject3.put("id", "order_amount");
        jsonObject3.put("name", "当日交易额");
        jsonObject3.put("value", todayOrderlCount);

        result.add(jsonObject1);

        result.add(jsonObject2);

        return result;
    }

    @RequestMapping(value = "/realtime-hours")
    public Object getTodayAndYesterday(String id, String date) {
        String yesterday = LocalDate.parse(date).minusDays(1).toString();
        JSONObject result = new JSONObject();
        System.out.println(yesterday);
        if (id.equals("dau")) {
            List<DUAData> today = duAService.getDUAPerHourDay(date);
            List<DUAData> yesterdayData = duAService.getDUAPerHourDay(yesterday);
            result.put("today", parseDAUDatas(today));
            result.put("yesterday", parseDAUDatas(yesterdayData));

        }
        if (id.equals("order_amount")) {
            List<GMVData> today = gmvService.getGMVPerHourByDay(date);
            List<GMVData> yesterdayData = gmvService.getGMVPerHourByDay(yesterday);

            result.put("today", parseGMVDatas(today));
            result.put("yesterday", parseGMVDatas(yesterdayData));
        }
        return result;
    }

    public JSONObject parseGMVDatas(List<GMVData> datas) {
        JSONObject result = new JSONObject();
        if (datas.size() == 0) {
            return result;
        }

        for (GMVData data : datas) {
            result.put(data.getHour(), data.getCount());
        }
        return result;
    }

    public JSONObject parseDAUDatas(List<DUAData> datas) {
        JSONObject result = new JSONObject();
        if (datas.size() == 0) {
            return result;
        }

        for (DUAData data : datas) {
            result.put(data.getHour(), data.getCount());
        }
        return result;
    }


}
